package portlet.ctcbtt;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.TimeZone;

import javax.faces.component.UICommand;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.layout.RichPanelBorderLayout;
import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;
import oracle.adf.view.rich.component.rich.nav.RichGoLink;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.jbo.Row;
import oracle.jbo.RowSet;
import oracle.jbo.domain.ClobDomain;
import oracle.jbo.domain.Number;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

import portlet.common.Utils;
import portlet.common.search.AdvancedSearchGroup;
import portlet.common.search.AdvancedSearchItem;


public class ChitietCbtt extends ADFBridgePortlet {
    private RichPanelGroupLayout danhSachPanel;
    private RichPanelGroupLayout pagingPanel;

    protected String getTitle(RenderRequest request) {
        // Get the customized title. This defaults to the declared title.
        PortletPreferences prefs = request.getPreferences();
        return prefs.getValue(PORTLETTITLE_KEY, super.getTitle(request));
    }
    public static final String APPLY_ACTION = "apply_action";
    public static final String OK_ACTION = "ok_action";
    public static final String PORTLETTITLE_KEY = "portletTitle";
    private static final String DEFAULT_LANGCODE = "vi";
    private final String LINK_DES = "des";
    private final String ROW_KEY = "rowkey";
    private final int DEFAULT_SIZE = 5;

    private String[] buildValueArray(String values) {
        if (values.indexOf(',') < 0) {
            return new String[] { values };
        }
        StringTokenizer st = new StringTokenizer(values, ",");
        String[] valueArray = new String[st.countTokens()];
        int i = 0;
        while (st.hasMoreTokens()) {
            valueArray[i] = st.nextToken().trim();
            i++;
        }
        return valueArray;
    }

    public void processAction(ActionRequest request,
                              ActionResponse response) throws PortletException,
                                                              IOException {
        request.setAttribute("javax.portlet.config", getPortletConfig());
        super.processAction(request, response);
    }

    public void render(RenderRequest request,
                       RenderResponse response) throws PortletException,
                                                       IOException {
        request.setAttribute("javax.portlet.config", getPortletConfig());
        super.render(request, response);
    }

    @Override
    protected void doView(RenderRequest renderRequest,
                          RenderResponse renderResponse) throws PortletException,
                                                                IOException {
        renderRequest.getPortletSession().setAttribute("reset", 1);
        checkParameter(renderRequest);
        super.doView(renderRequest, renderResponse);
    }

    public boolean checkParameter(RenderRequest request) {
        boolean check = true;
        String pLoaiDonViId = request.getParameter("loaidonviid");
        String pLangCode = request.getParameter("langcode");
        String pChiTietUrl = request.getParameter("chitieturl");
        String pLoaiCbttId = request.getParameter("loaicbttid");
        String pSoLuongBanGhi = request.getParameter("soluongbanghi");
        String pCbttId = request.getParameter("cbttid");
        String pHtFileServletUrl = request.getParameter("htfileservleturl");


        String sLoaiDonViId =
            (String)request.getPortletSession().getAttribute("loaidonviid");
        String sLangCode =
            (String)request.getPortletSession().getAttribute("langcode");
        String sChiTietUrl =
            (String)request.getPortletSession().getAttribute("chitieturl");
        String sLoaiCbttId =
            (String)request.getPortletSession().getAttribute("loaicbttid");
        String sSoLuongBanGhi =
            (String)request.getPortletSession().getAttribute("soluongbanghi");
        String sCbttId =
            (String)request.getPortletSession().getAttribute("cbttid");
        String sHtFileServletUrl =
            (String)request.getPortletSession().getAttribute("htfileservleturl");
        if (pLangCode == null) {
            pLangCode = DEFAULT_LANGCODE;
        }
        if (sLoaiDonViId != null && pLoaiDonViId != null) {
            if (sLoaiDonViId.equals(pLoaiDonViId) == false) {
                check = false;
            }
        } else if (pLoaiDonViId != null) {
            check = false;
        } else if (sLoaiDonViId != null) {
            check = false;
        }

        if (sLangCode != null && pLangCode != null) {
            if (sLangCode.equals(pLangCode) == false) {
                check = false;
            }
        } else if (pLangCode != null) {
            check = false;
        } else if (sLangCode != null) {
            check = false;
        }

        if (sChiTietUrl != null && pChiTietUrl != null) {
            if (sChiTietUrl.equals(pChiTietUrl) == false) {
                check = false;
            }
        } else if (pChiTietUrl != null) {
            check = false;
        } else if (sChiTietUrl != null) {
            check = false;
        }

        if (sLoaiCbttId != null && pLoaiCbttId != null) {
            if (sLoaiCbttId.equals(pLoaiCbttId) == false) {
                check = false;
            }
        } else if (pLoaiCbttId != null) {
            check = false;
        } else if (sLoaiCbttId != null) {
            check = false;
        }

        if (sSoLuongBanGhi != null && pSoLuongBanGhi != null) {
            if (sSoLuongBanGhi.equals(pSoLuongBanGhi) == false) {
                check = false;
            }
        } else if (pSoLuongBanGhi != null) {
            check = false;
        } else if (sSoLuongBanGhi != null) {
            check = false;
        }

        if (sCbttId != null && pCbttId != null) {
            if (sCbttId.equals(pCbttId) == false) {
                check = false;
            }
        } else if (pCbttId != null) {
            check = false;
        } else if (sCbttId != null) {
            check = false;
        }

        if (sHtFileServletUrl != null && pHtFileServletUrl != null) {
            if (sHtFileServletUrl.equals(pHtFileServletUrl) == false) {
                check = false;
            }
        } else if (pHtFileServletUrl != null) {
            check = false;
        } else if (sHtFileServletUrl != null) {
            check = false;
        }
        request.getPortletSession().setAttribute("loaidonviid", pLoaiDonViId);
        request.getPortletSession().setAttribute("langcode", pLangCode);
        request.getPortletSession().setAttribute("chitieturl", pChiTietUrl);
        request.getPortletSession().setAttribute("loaicbttid", pLoaiCbttId);
        request.getPortletSession().setAttribute("soluongbanghi",
                                                 pSoLuongBanGhi);
        request.getPortletSession().setAttribute("cbttid", pCbttId);
        request.getPortletSession().setAttribute("htfileservleturl",
                                                 pHtFileServletUrl);
        return check;
    }

    public String getResetForm() {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Integer reset =
            (Integer)request.getPortletSession().getAttribute("reset");
        if (reset != null && reset.intValue() == 1) {
            resetIterator(request);
            createForm();
            request.getPortletSession().setAttribute("reset", 0);
            AdfFacesContext.getCurrentInstance().addPartialTarget(danhSachPanel);
        }
        return null;
    }

    private void createForm() {
        danhSachPanel.getChildren().clear();
        pagingPanel.getChildren().clear();
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String chiTietUrl =
            (String)request.getPortletSession().getAttribute("chitieturl");
        String loaiCbttId =
            (String)request.getPortletSession().getAttribute("loaicbttid");
        String loaiDonViId =
            (String)request.getPortletSession().getAttribute("loaidonviid");
        String langCode =
            (String)request.getPortletSession().getAttribute("langcode");
        if (langCode == null) {
            langCode = DEFAULT_LANGCODE;
        }
        Number donviid = null;
        Map<String, String> mapTruongTtRoot = new HashMap();
        DCIteratorBinding truongTtView1Iterator =
            Utils.getIteratorBinding("DlctTruongTtView1Iterator");
        truongTtView1Iterator.setRangeSize(-1);
        DCIteratorBinding cbttView1Iterator =
            Utils.getIteratorBinding("DlctCbttView1Iterator");
        String cbttRowKey =
            (String)request.getPortletSession().getAttribute("cbttid");
        if (cbttView1Iterator.findRowByKeyString(cbttRowKey) != null) {
            try {
                cbttView1Iterator.setCurrentRowWithKey(cbttRowKey);
                HashMap<String, Object> mapValue = new HashMap();
                String rowKey =
                    cbttView1Iterator.getCurrentRow().getKey().toStringFormat(false);
                mapValue.put(ROW_KEY, rowKey);
                RowSet rowSetGtTt =
                    (RowSet)cbttView1Iterator.getCurrentRow().getAttribute("DlctGtCbttTtView");
                if (rowSetGtTt != null) {
                    rowSetGtTt.setRangeSize(-1);
                    Row[] rowsGtTt = rowSetGtTt.getAllRowsInRange();
                    if (rowsGtTt == null) {
                        rowsGtTt = new Row[] { };
                    }
                    for (int j = 0; j < rowsGtTt.length; j++) {
                        Row truongTtRow =
                            (Row)rowsGtTt[j].getAttribute("DlctTruongTtView");
                        if (truongTtRow != null) {
                            Number id = (Number)truongTtRow.getAttribute("Id");
                            Number kieuDuLieu =
                                (Number)truongTtRow.getAttribute("KieuDuLieu");
                            if (id != null && kieuDuLieu != null) {
                                int kieu = kieuDuLieu.intValue();
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                    Number valNumber =
                                        (Number)rowsGtTt[j].getAttribute("ValNumber");
                                    if (valNumber != null) {
                                        mapValue.put(id.toString(),
                                                     valNumber.toString());
                                    }
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                    String valVarchar =
                                        (String)rowsGtTt[j].getAttribute("ValVarchar");
                                    if (valVarchar != null) {
                                        mapValue.put(id.toString(),
                                                     valVarchar);
                                    }
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                                    String valVarchar =
                                        (String)rowsGtTt[j].getAttribute("ValVarchar");
                                    if (valVarchar != null) {
                                        String link = valVarchar.toLowerCase();
                                        if (link.startsWith("http://") ==
                                            false) {
                                            link = "http://" + link;
                                        }
                                        mapValue.put(id.toString() + LINK_DES,
                                                     link);
                                        mapValue.put(id.toString(),
                                                     valVarchar);
                                    }
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                                    String valVarchar =
                                        (String)rowsGtTt[j].getAttribute("ValVarchar");
                                    if (valVarchar != null) {
                                        String link =
                                            "mailto:" + valVarchar.toLowerCase();
                                        mapValue.put(id.toString() + LINK_DES,
                                                     link);
                                        mapValue.put(id.toString(),
                                                     valVarchar);
                                    }
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                    oracle.jbo.domain.Date valDate =
                                        (oracle.jbo.domain.Date)rowsGtTt[j].getAttribute("ValDate");
                                    if (valDate != null) {
                                        java.util.Date date =
                                            valDate.dateValue();
                                        if (date != null) {
                                            mapValue.put(id.toString(), date);
                                        }
                                    }
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                    ClobDomain valClob =
                                        (ClobDomain)rowsGtTt[j].getAttribute("ValClob");
                                    if (valClob != null) {
                                        mapValue.put(id.toString(),
                                                     valClob.toString());
                                    }
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                    Row fileViewRow =
                                        (Row)rowsGtTt[j].getAttribute("HtFileView");
                                    if (fileViewRow != null) {
                                        mapValue.put(id.toString(),
                                                     fileViewRow);
                                    }
                                }
                            }
                        }
                    }
                }
                List<UIComponent> listTieuDe = new ArrayList();
                List<UIComponent> listNgayThang = new ArrayList();
                List<UIComponent> listChiTiet = new ArrayList();
                List<UIComponent> listFile = new ArrayList();

                String donvi = (String)mapValue.get("donvi");
                if (donvi != null) {
                    RichOutputText outputText = new RichOutputText();
                    outputText.setValue(donvi);
                    listTieuDe.add(outputText);
                }

                Row[] rowsTruongTt = truongTtView1Iterator.getAllRowsInRange();
                if (rowsTruongTt == null) {
                    rowsTruongTt = new Row[] { };
                }
                for (int j = 0; j < rowsTruongTt.length; j++) {
                    String id =
                        ((Number)rowsTruongTt[j].getAttribute("Id")).toString();
                    Number kieuDuLieu =
                        (Number)rowsTruongTt[j].getAttribute("KieuDuLieu");
                    Number viTriHienThi =
                        (Number)rowsTruongTt[j].getAttribute("ViTriHienThi");
                    Row loopRow = rowsTruongTt[j];
                    Row rootRow = null;
                    while (loopRow != null) {
                        rootRow = loopRow;
                        loopRow =
                                (Row)loopRow.getAttribute("DlctTruongTtTuIdDlctTruongTtView");
                    }
                    String rootId =
                        ((Number)rootRow.getAttribute("Id")).toString();
                    if (viTriHienThi != null && kieuDuLieu != null) {
                        int kieu = kieuDuLieu.intValue();
                        int viTri = viTriHienThi.intValue();
                        mapTruongTtRoot.put(id, rootId);
                        if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TIN_TIEU_DE.getId() &
                             viTri) ==
                            Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TIN_TIEU_DE.getId()) {
                            Object obj = mapValue.get(rootId);
                            if (obj != null) {
                                RichOutputText outputText =
                                    new RichOutputText();
                                outputText.setStyleClass("cbtt-news-detail-title");
                                outputText.setValue(obj);
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                    DateTimeConverter converter =
                                        (DateTimeConverter)FacesContext.getCurrentInstance().getApplication().createConverter("javax.faces.DateTime");
                                    converter.setLocale(new Locale(langCode));
                                    converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                                    converter.setType("date");
                                    converter.setDateStyle("short");
                                    outputText.setConverter(converter);
                                }
                                listTieuDe.add(outputText);
                            }

                        }
                        if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TIN_INFO.getId() &
                             viTri) ==
                            Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TIN_INFO.getId()) {
                            Object obj = mapValue.get(rootId);
                            if (obj != null) {
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                    String htFileServletUrl =
                                        (String)request.getPortletSession().getAttribute("htfileservleturl");
                                    Row row = (Row)obj;
                                    RichGoLink goLink = new RichGoLink();
                                    goLink.setTargetFrame("_parent");
                                    goLink.setInlineStyle("padding:5px 7px;");
                                    goLink.setText("• " +
                                                   (String)row.getAttribute("TenFile"));
                                    goLink.setDestination(htFileServletUrl +
                                                          "?id=" +
                                                          row.getAttribute("Id"));
                                    listFile.add(goLink);
                                } else if (kieu ==
                                           Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                    RichOutputText outputFormatted =
                                        new RichOutputText();
                                    outputFormatted.setEscape(false);
                                    outputFormatted.setValue(obj.toString());
                                    listChiTiet.add(outputFormatted);
                                } else {
                                    RichOutputText outputText =
                                        new RichOutputText();
                                    outputText.setValue(obj);
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                        DateTimeConverter converter =
                                            (DateTimeConverter)FacesContext.getCurrentInstance().getApplication().createConverter("javax.faces.DateTime");
                                        converter.setLocale(new Locale(langCode));
                                        converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                                        converter.setType("date");
                                        converter.setDateStyle("short");
                                        outputText.setConverter(converter);
                                    }
                                    listChiTiet.add(outputText);
                                }
                            }
                        }
                    }
                }
                RichOutputText outputText = new RichOutputText();
                outputText.setValue(" (");
                listNgayThang.add(outputText);
                Object obj =
                    cbttView1Iterator.getCurrentRow().getAttribute("NgayTao");
                if (obj != null) {
                    oracle.jbo.domain.Date valDate =
                        (oracle.jbo.domain.Date)obj;
                    if (valDate != null) {
                        java.util.Date date = valDate.dateValue();
                        if (date != null) {
                            outputText = new RichOutputText();
                            outputText.setValue(date);
                            DateTimeConverter converter =
                                (DateTimeConverter)FacesContext.getCurrentInstance().getApplication().createConverter("javax.faces.DateTime");
                            converter.setLocale(new Locale(langCode));
                            converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                            converter.setType("date");
                            converter.setDateStyle("short");
                            outputText.setConverter(converter);
                            outputText.setValue(outputText.getValue());
                            listNgayThang.add(outputText);
                        }
                    }
                }
                outputText = new RichOutputText();
                outputText.setValue(")");
                listNgayThang.add(outputText);
                RichPanelBorderLayout borderLayout =
                    new RichPanelBorderLayout();
                borderLayout.setStyleClass("cbtt-news-detail-title-box");
                RichPanelGroupLayout groupLayout = new RichPanelGroupLayout();
                groupLayout.setLayout(groupLayout.LAYOUT_VERTICAL);
                borderLayout.getChildren().addAll(listTieuDe);
                borderLayout.getChildren().addAll(listNgayThang);
                groupLayout.getChildren().addAll(listChiTiet);
                if (listFile.size() > 0) {
                    outputText = new RichOutputText();
                    outputText.setEscape(false);
                    outputText.setValue("<div class='cbtt-news-detail-othertitle'>" +
                                        getResource("FILE_DINH_KEM") +
                                        "</div>");
                    groupLayout.getChildren().add(outputText);
                }
                groupLayout.getChildren().addAll(listFile);
                danhSachPanel.getChildren().add(borderLayout);
                danhSachPanel.getChildren().add(groupLayout);


                donviid =
                        (Number)cbttView1Iterator.getCurrentRow().getAttribute("DlctDonViId");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {

                List<AdvancedSearchGroup> listASG = new ArrayList();
                AdvancedSearchGroup asgTrangThai = new AdvancedSearchGroup();
                listASG.add(asgTrangThai);
                AdvancedSearchItem asiTrangThai = new AdvancedSearchItem();
                asgTrangThai.getListChildren().add(asiTrangThai);
                asiTrangThai.setItemName("TrangThai");
                asiTrangThai.setRequired(true);
                asiTrangThai.getListValue().add(0,
                                                new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
                if (loaiCbttId != null) {
                    try {
                        Number loaiCbttNumber = new Number(loaiCbttId);
                        AdvancedSearchGroup asg = new AdvancedSearchGroup();
                        listASG.add(asg);
                        AdvancedSearchItem asiLoaiCbtt =
                            new AdvancedSearchItem();
                        asg.getListChildren().add(asiLoaiCbtt);
                        asiLoaiCbtt.setItemName("DlctLoaiCbtt");
                        asiLoaiCbtt.getListValue().add(loaiCbttNumber);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (donviid != null) {
                    try {
                        AdvancedSearchGroup asg = new AdvancedSearchGroup();
                        listASG.add(asg);
                        AdvancedSearchItem asiLoaiCbtt =
                            new AdvancedSearchItem();
                        asg.getListChildren().add(asiLoaiCbtt);
                        asiLoaiCbtt.setItemName("DlctDonViId");
                        asiLoaiCbtt.getListValue().add(donviid);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (langCode != null) {
                    try {
                        AdvancedSearchGroup asg = new AdvancedSearchGroup();
                        listASG.add(asg);
                        AdvancedSearchItem asi = new AdvancedSearchItem();
                        asg.getListChildren().add(asi);
                        asi.setItemName("DlctCbttNnView");
                        asi.setRequired(true);
                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                        AdvancedSearchGroup asgChild =
                            new AdvancedSearchGroup();
                        asi.getListChildren().add(asgChild);
                        AdvancedSearchItem asiChild = new AdvancedSearchItem();
                        asgChild.getListChildren().add(asiChild);
                        asiChild.setItemName("DlctNgonNguView");
                        asiChild.setRequired(true);
                        asiChild.setOperator(AdvancedSearchItem.OPER_EXISTS);
                        AdvancedSearchGroup asgChild2 =
                            new AdvancedSearchGroup();
                        asiChild.getListChildren().add(asgChild2);
                        AdvancedSearchItem asiChild2 =
                            new AdvancedSearchItem();
                        asgChild2.getListChildren().add(asiChild2);
                        asiChild2.setItemName("Ma");
                        asiChild2.setRequired(true);
                        asiChild2.getListValue().add(langCode);
                    } catch (Exception e) {
                    }
                }
                Utils.searchadvanced("DlctCbttView1Iterator", listASG);
                int size = DEFAULT_SIZE;
                try {
                    size =
Integer.parseInt((String)request.getPortletSession().getAttribute("soluongbanghi"));
                } catch (Exception e) {

                }
                cbttView1Iterator.setRangeSize(size + 1);
                Row[] rows = cbttView1Iterator.getAllRowsInRange();
                if (rows == null) {
                    rows = new Row[] { };
                }
                for (int i = 0, l = 0; i < rows.length && l < size; i++, l++) {
                    Row row = cbttView1Iterator.getRowAtRangeIndex(i);
                    if (row.getKey().toStringFormat(false).equals(cbttRowKey)) {
                        continue;
                    }
                    HashMap<String, Object> mapValue = new HashMap();
                    String rowKey = row.getKey().toStringFormat(false);
                    mapValue.put(ROW_KEY, rowKey);
                    RowSet rowSetGtTt =
                        (RowSet)row.getAttribute("DlctGtCbttTtView");
                    if (rowSetGtTt != null) {
                        rowSetGtTt.setRangeSize(-1);
                        Row[] rowsGtTt = rowSetGtTt.getAllRowsInRange();
                        if (rowsGtTt == null) {
                            rowsGtTt = new Row[] { };
                        }
                        for (int j = 0; j < rowsGtTt.length; j++) {
                            Row truongTtRow =
                                (Row)rowsGtTt[j].getAttribute("DlctTruongTtView");
                            if (truongTtRow != null) {
                                Number id =
                                    (Number)truongTtRow.getAttribute("Id");
                                Number kieuDuLieu =
                                    (Number)truongTtRow.getAttribute("KieuDuLieu");
                                if (id != null && kieuDuLieu != null) {
                                    int kieu = kieuDuLieu.intValue();
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                        Number valNumber =
                                            (Number)rowsGtTt[j].getAttribute("ValNumber");
                                        if (valNumber != null) {
                                            mapValue.put(id.toString(),
                                                         valNumber.toString());
                                        }
                                    }
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                        String valVarchar =
                                            (String)rowsGtTt[j].getAttribute("ValVarchar");
                                        if (valVarchar != null) {
                                            mapValue.put(id.toString(),
                                                         valVarchar);
                                        }
                                    }
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                                        String valVarchar =
                                            (String)rowsGtTt[j].getAttribute("ValVarchar");
                                        if (valVarchar != null) {
                                            String link =
                                                valVarchar.toLowerCase();
                                            if (link.startsWith("http://") ==
                                                false) {
                                                link = "http://" + link;
                                            }
                                            mapValue.put(id.toString() +
                                                         LINK_DES, link);
                                            mapValue.put(id.toString(),
                                                         valVarchar);
                                        }
                                    }
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                                        String valVarchar =
                                            (String)rowsGtTt[j].getAttribute("ValVarchar");
                                        if (valVarchar != null) {
                                            String link =
                                                "mailto:" + valVarchar.toLowerCase();
                                            mapValue.put(id.toString() +
                                                         LINK_DES, link);
                                            mapValue.put(id.toString(),
                                                         valVarchar);
                                        }
                                    }
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                        oracle.jbo.domain.Date valDate =
                                            (oracle.jbo.domain.Date)rowsGtTt[j].getAttribute("ValDate");
                                        if (valDate != null) {
                                            java.util.Date date =
                                                valDate.dateValue();
                                            if (date != null) {
                                                mapValue.put(id.toString(),
                                                             date);
                                            }
                                        }
                                    }
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                        ClobDomain valClob =
                                            (ClobDomain)rowsGtTt[j].getAttribute("ValClob");
                                        if (valClob != null) {
                                            mapValue.put(id.toString(),
                                                         valClob.toString());
                                        }
                                    }
                                    if (kieu ==
                                        Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                        Row fileViewRow =
                                            (Row)rowsGtTt[j].getAttribute("HtFileView");
                                        if (fileViewRow != null) {
                                            mapValue.put(id.toString(),
                                                         fileViewRow);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    List<UIComponent> listTieuDe = new ArrayList();
                    List<UIComponent> listNgayThang = new ArrayList();

                    Row[] rowsTruongTt =
                        truongTtView1Iterator.getAllRowsInRange();
                    if (rowsTruongTt == null) {
                        rowsTruongTt = new Row[] { };
                    }
                    for (int j = 0; j < rowsTruongTt.length; j++) {
                        String id =
                            ((Number)rowsTruongTt[j].getAttribute("Id")).toString();
                        Number kieuDuLieu =
                            (Number)rowsTruongTt[j].getAttribute("KieuDuLieu");
                        Number viTriHienThi =
                            (Number)rowsTruongTt[j].getAttribute("ViTriHienThi");
                        Row loopRow = rowsTruongTt[j];
                        Row rootRow = null;
                        while (loopRow != null) {
                            rootRow = loopRow;
                            loopRow =
                                    (Row)loopRow.getAttribute("DlctTruongTtTuIdDlctTruongTtView");
                        }
                        String rootId =
                            ((Number)rootRow.getAttribute("Id")).toString();
                        if (viTriHienThi != null && kieuDuLieu != null) {
                            int viTri = viTriHienThi.intValue();
                            mapTruongTtRoot.put(id, rootId);
                            if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TIN_TIEU_DE.getId() &
                                 viTri) ==
                                Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TIN_TIEU_DE.getId()) {
                                Object obj = mapValue.get(rootId);
                                if (obj != null) {
                                    RichGoLink goLink = new RichGoLink();
                                    goLink.setTargetFrame("_parent");
                                    goLink.setText(obj.toString());
                                    goLink.setDestination(chiTietUrl +
                                                          "?cbttid=" + rowKey +
                                                          (loaiDonViId ==
                                                           null ? "" :
                                                           "&loaidonviid=" +
                                                           loaiDonViId) +
                                                          (langCode == null ?
                                                           "" :
                                                           "&langcode=" + langCode));
                                    listTieuDe.add(goLink);
                                }
                            }
                        }
                    }
                    RichOutputText outputText = new RichOutputText();
                    outputText.setValue(" (");
                    listNgayThang.add(outputText);
                    Object obj = row.getAttribute("NgayTao");
                    if (obj != null) {
                        oracle.jbo.domain.Date valDate =
                            (oracle.jbo.domain.Date)obj;
                        if (valDate != null) {
                            java.util.Date date = valDate.dateValue();
                            if (date != null) {
                                outputText = new RichOutputText();
                                outputText.setValue(date);
                                DateTimeConverter converter =
                                    (DateTimeConverter)FacesContext.getCurrentInstance().getApplication().createConverter("javax.faces.DateTime");
                                converter.setLocale(new Locale(langCode));
                                converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                                converter.setType("date");
                                converter.setDateStyle("short");
                                outputText.setConverter(converter);
                                outputText.setValue(outputText.getValue());
                                listNgayThang.add(outputText);
                            }
                        }
                    }
                    outputText = new RichOutputText();
                    outputText.setValue(")");
                    listNgayThang.add(outputText);
                    RichPanelBorderLayout borderLayout =
                        new RichPanelBorderLayout();
                    borderLayout.setStyleClass("cbtt-related-news-title-item ");
                    borderLayout.getChildren().addAll(listTieuDe);
                    borderLayout.getChildren().addAll(listNgayThang);
                    pagingPanel.getChildren().add(borderLayout);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        AdfFacesContext.getCurrentInstance().addPartialTarget(danhSachPanel);
    }

    private void resetIterator(PortletRequest request) {
        String loaiCbttId =
            (String)request.getPortletSession().getAttribute("loaicbttid");
        String langCode =
            (String)request.getPortletSession().getAttribute("langcode");
        if (langCode == null) {
            langCode = DEFAULT_LANGCODE;
        }

        List<AdvancedSearchGroup> listASG = new ArrayList();
        AdvancedSearchGroup asgTrangThai = new AdvancedSearchGroup();
        listASG.add(asgTrangThai);
        AdvancedSearchItem asiTrangThai = new AdvancedSearchItem();
        asgTrangThai.getListChildren().add(asiTrangThai);
        asiTrangThai.setItemName("TrangThai");
        asiTrangThai.setRequired(true);
        asiTrangThai.getListValue().add(0,
                                        new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
        if (loaiCbttId != null) {
            try {
                Number loaiCbttNumber = new Number(loaiCbttId);
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asiLoaiCbtt = new AdvancedSearchItem();
                asg.getListChildren().add(asiLoaiCbtt);
                asiLoaiCbtt.setItemName("DlctLoaiCbtt");
                asiLoaiCbtt.getListValue().add(loaiCbttNumber);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (langCode != null) {
            try {
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("DlctCbttNnView");
                asi.setRequired(true);
                asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild = new AdvancedSearchGroup();
                asi.getListChildren().add(asgChild);
                AdvancedSearchItem asiChild = new AdvancedSearchItem();
                asgChild.getListChildren().add(asiChild);
                asiChild.setItemName("DlctNgonNguView");
                asiChild.setRequired(true);
                asiChild.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild2 = new AdvancedSearchGroup();
                asiChild.getListChildren().add(asgChild2);
                AdvancedSearchItem asiChild2 = new AdvancedSearchItem();
                asgChild2.getListChildren().add(asiChild2);
                asiChild2.setItemName("Ma");
                asiChild2.setRequired(true);
                asiChild2.getListValue().add(langCode);
            } catch (Exception e) {
            }
        }
        Utils.searchadvanced("DlctCbttView1Iterator", listASG);

        listASG = new ArrayList();
        asgTrangThai = new AdvancedSearchGroup();
        listASG.add(asgTrangThai);
        asiTrangThai = new AdvancedSearchItem();
        asgTrangThai.getListChildren().add(asiTrangThai);
        asiTrangThai.setItemName("TrangThai");
        asiTrangThai.setRequired(true);
        asiTrangThai.getListValue().add(0,
                                        new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
        AdvancedSearchGroup asgLoaiTruongTt = new AdvancedSearchGroup();
        listASG.add(asgLoaiTruongTt);
        AdvancedSearchItem asiLoaiTruongTt = new AdvancedSearchItem();
        asgLoaiTruongTt.getListChildren().add(asiLoaiTruongTt);
        asiLoaiTruongTt.setItemName("DlctLoaiTruongTt");
        asiLoaiTruongTt.setRequired(true);
        asiLoaiTruongTt.getListValue().add(0,
                                           new Number(Utils.ENUM_LOAI_TRUONG_TT.CONG_BO_THONG_TIN.getId()));
        if (langCode != null) {
            try {
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("DlctNgonNguView");
                asi.setRequired(true);
                asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild = new AdvancedSearchGroup();
                asi.getListChildren().add(asgChild);
                AdvancedSearchItem asiChild = new AdvancedSearchItem();
                asgChild.getListChildren().add(asiChild);
                asiChild.setItemName("Ma");
                asiChild.setRequired(true);
                asiChild.getListValue().add(langCode);
            } catch (Exception e) {
            }
        }
        Utils.searchadvanced("DlctTruongTtView1Iterator", listASG);
    }

    private String getResource(String resourceKey) {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String langCode =
            (String)request.getPortletSession().getAttribute("langcode");
        if (langCode == null) {
            langCode = DEFAULT_LANGCODE;
        }
        Locale locale = new Locale(langCode);
        ResourceBundle bundle =
            ResourceBundle.getBundle("portlet.ctcbtt.resource.ChitietCbttBundle",
                                     locale);
        return bundle.getString(resourceKey);
    }

    public String getTieuDe() {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        return request.getParameter("tieude");
    }

    public void setDanhSachPanel(RichPanelGroupLayout danhSachPanel) {
        this.danhSachPanel = danhSachPanel;
    }

    public RichPanelGroupLayout getDanhSachPanel() {
        return danhSachPanel;
    }

    public void setPagingPanel(RichPanelGroupLayout pagingPanel) {
        this.pagingPanel = pagingPanel;
    }

    public RichPanelGroupLayout getPagingPanel() {
        return pagingPanel;
    }

    public String getTinkhacLabel() {
        return getResource("CAC_TIN_KHAC");
    }
}
